using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Brick;

namespace Assets.Scripts.GDK
{
	public class gdk_coroutine
	{
		private gdk_coroutine_component coroutineComponent;

		public static gdk_coroutine _instance;

		private LinkedList<IEnumerator> coroutineList = new LinkedList<IEnumerator>();

		public static gdk_coroutine Instance
		{
			get
			{
				if (_instance == null)
				{
					_instance = new gdk_coroutine();
				}
				return _instance;
			}
			set
			{
				_instance = value;
			}
		}

		public gdk_coroutine()
		{
			GameObject gameObject = GameObject.CreatePrimitive(PrimitiveType.Plane);
			gameObject.name = "[coroutineNode]";
			UnityEngine.Object.DontDestroyOnLoad(gameObject);
			coroutineComponent = gameObject.AddComponent<gdk_coroutine_component>();
		}

		public IEnumerator StartCoroutine(IEnumerator ie)
		{
			coroutineList.AddLast(ie);
			return ie;
		}

		public void StopCoroutine(IEnumerator ie)
		{
			try
			{
				coroutineList.Remove(ie);
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.ToString());
			}
		}

		public void Update()
		{
			for (LinkedListNode<IEnumerator> linkedListNode = coroutineList.First; linkedListNode != null; linkedListNode = linkedListNode.Next)
			{
				IEnumerator value = linkedListNode.Value;
				bool flag = true;
				if (value.Current is gdk_iWait)
				{
					gdk_iWait gdk_iWait = (gdk_iWait)value.Current;
					if (gdk_iWait.Tick())
					{
						flag = value.MoveNext();
					}
				}
				else
				{
					flag = value.MoveNext();
				}
				if (!flag)
				{
					coroutineList.Remove(linkedListNode);
				}
			}
		}
	}
}
